package view;

import service.ClientThread;
import service.UserService;
import view.components.Avatar;
import common.Message;
import common.MessageType;
import entity.User;
import tools.ThreadManage;
import view.components.FriendPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

public class FriendList extends JFrame {

    private static final long serialVersionUID = 1L;

    private static int windowsWedth = 280;
    private static int windowsHeight = 600;
    private static Container container;// 本窗口面板
    private static JPanel friendList;// 好友列表
    private static JScrollPane jScrollPane;
    private JPanel bottom;// 底部面板
    private JButton addFriend;// 添加好友
    private User user;// 用户对象

    public FriendList(User user) {

        this.user = user;
        this.setSize(windowsWedth, windowsHeight);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 获取本窗体容器设置布局
        container = this.getContentPane();
        container.setLayout(null);

        // qq头像
        JLabel jbl_photo = new Avatar(user.getAvatarID(), 80, 80);
        jbl_photo.setBounds(20, 22, 80, 80);
        container.add(jbl_photo);

        // qq昵称
        JLabel jbl_qqName = new JLabel(user.getNickname() + "(" + user.getQq() + ")");
        jbl_qqName.setForeground(Color.white);
        jbl_qqName.setFont(new Font("", Font.BOLD, 18));
        jbl_qqName.setBounds(120, 30, 140, 20);
        container.add(jbl_qqName);

        // 签名
        JTextField sign = new JTextField();
        sign.setBounds(120, 60, 140, 22);
        sign.setBackground(new Color(3, 37, 108));
        sign.setForeground(Color.white);
        sign.setBorder(null);
        sign.setText(user.getSign());
        container.add(sign);

        //上半部分背景图
        JLabel jbl_background = new JLabel();
        jbl_background.setBackground(new Color(3, 37, 108));
        jbl_background.setOpaque(true);
        jbl_background.setBounds(0, 0, windowsWedth, 128);
        container.add(jbl_background);

        // 得到显示器屏幕的宽高设置窗体居中
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setBounds((width - windowsWedth) / 2, (height - windowsHeight) / 2, 0, 0);
        this.setMinimumSize(new Dimension(windowsWedth, windowsHeight));

        // 添加好友按钮
        addFriend = new JButton("添加好友");
        addFriend.setBackground(new Color(3, 37, 108));
        addFriend.setForeground(Color.white);
        addFriend.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddFriend();
            }
        });

        // 窗体底部面板
        bottom = new JPanel(new GridLayout(1, 0));
        bottom.setBounds(0, 542, windowsWedth, 30);
        bottom.add(addFriend);
        container.add(bottom);


        // 窗体关闭事件
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                // 发送下线消息
                Message message = new Message(MessageType.OFFLINE);
                message.setContent(user);
                ClientThread thread = ThreadManage.getThread(user.getQq());
                thread.send(message);
            }
        });

        //设置窗体可见
//        this.setResizable(false);
        this.setVisible(true);
    }

    public void updateFriendList() {
        // 向服务器请求好友列表
        Message message = new Message();
        message.setType(MessageType.GET_FRIENDS);
        message.setContent(user);
        ThreadManage.getThread(user.getQq()).send(message);

        friendList = new JPanel();
        friendList.setLayout(null);

        if (jScrollPane != null) {
            container.remove(jScrollPane);
        }
        jScrollPane = new JScrollPane();
        jScrollPane.setLocation(0, 128);
        jScrollPane.setViewportView(friendList);
        jScrollPane.setSize(windowsWedth, windowsHeight - 184);
        jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);// 需要时显示（默认）
        jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);// 从不显示
        container.add(jScrollPane);
    }

    public void showFriendList(ArrayList<User> list) {
        int panelHeight = 60;
        friendList.removeAll();

        for (int i = 0; i < list.size(); i++) {
            User friend = list.get(i);
            // 创建好友面板
            JPanel friendPanel = new FriendPanel(this.user, friend);
            friendPanel.setLocation(0, i * panelHeight);
            friendList.add(friendPanel);
        }

        friendList.setPreferredSize(new Dimension(windowsWedth, list.size() * panelHeight));
        container.repaint();
    }

    class AddFriend extends JFrame implements ActionListener {
        public AddFriend() {
            initComponents();
        }

        private void initComponents() {
            // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
            // Generated using JFormDesigner Evaluation license - unknown
            label1 = new JLabel();
            textField1 = new JTextField();
            button1 = new JButton();

            //======== this ========
            setVisible(true);
            Container contentPane = getContentPane();
            contentPane.setLayout(null);

            //---- label1 ----
            label1.setText("\u597d\u53cbQQ\u53f7");
            contentPane.add(label1);
            label1.setBounds(new Rectangle(new Point(70, 35), label1.getPreferredSize()));
            contentPane.add(textField1);
            textField1.setBounds(140, 30, 200, textField1.getPreferredSize().height);

            //---- button1 ----
            button1.setText("\u6dfb\u52a0");
            contentPane.add(button1);
            button1.setBackground(new Color(3, 37, 108));
            button1.setForeground(Color.white);
            button1.setBounds(160, 75, 80, button1.getPreferredSize().height);
            button1.addActionListener(this);

            contentPane.setPreferredSize(new Dimension(400, 140));
            pack();
            setLocationRelativeTo(getOwner());
            // JFormDesigner - End of component initialization  //GEN-END:initComponents
        }

        // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
        // Generated using JFormDesigner Evaluation license - unknown
        private JLabel label1;
        private JTextField textField1;
        private JButton button1;

        @Override
        public void actionPerformed(ActionEvent e) {
            String text = textField1.getText();
            if (text.equals("")) {
                JOptionPane.showMessageDialog(this, "请输入QQ号！");
            } else if (text.length() > 4) {
                JOptionPane.showMessageDialog(this, "请输入正确的QQ号！");
            } else {
                int fqq = Integer.parseInt(text);
                Boolean aBoolean = new UserService().addFriend(user.getQq(), fqq);
                if (aBoolean) {
                    JOptionPane.showMessageDialog(this, "添加成功！");
                    this.dispose();
                    updateFriendList();
                } else {
                    JOptionPane.showMessageDialog(this, "添加失败！");
                }
            }
        }
    }

}